<%@ page import="model.UserRank" %>
<%@ page import="server.RankServer" %>
<%@ page import="java.util.LinkedList" %>
<%@ page import="java.util.LinkedHashMap" %>
<%@ page import="config.ApplicationConfig" %>
<%@ page import="server.FileServer" %>
<%@ page import="server.UserServer" %>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="java.net.URLEncoder" %><%--
  Created by IntelliJ IDEA.
  User: tum0r
  Date: 2019-04-23
  Time: 13:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>排名</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/page.css">
    <link rel="stylesheet" href="css/rank.css">
</head>
<%
    LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
    request.setCharacterEncoding(pageConfig.get("encode"));
    response.setCharacterEncoding(pageConfig.get("encode"));
    response.addHeader("refresh", "60;URL=rank_dynamic.jsp");
%>
<body>
<div>
    <%
        RankServer rankServer = new RankServer();
        rankServer.open();
        LinkedList<UserRank> userRanks = rankServer.getUserRank();
        UserRank item;
        int i = 0;
        FileServer fileServer = new FileServer();
        fileServer.open();
        UserServer userServer = new UserServer();
        userServer.open();
        int count = 0;
        for (; i < userRanks.size() && i < 3; i++) {
            item = userRanks.get(i);
            if (item.getAllCount() != 0) {
                count++;
                JSONObject userInformation = userServer.searchUserByName(item.getUserName());
    %>
    <div class="col-sm-4 col-md-4">
        <a href="details.jsp?user=<%=URLEncoder.encode(item.getUserName())%>" class="thumbnail" target="_blank">
            <%
                if (userInformation.has("icon") && JSONObject.fromObject(userInformation.getString("icon")).has("uuid")) {
                    JSONObject icon = JSONObject.fromObject(userInformation.getString("icon"));
            %>
            <img src="<%=fileServer.getImage(icon.getString("uuid"))%>" class="img-circle" width=170px height=170px
                 style="border: darkorange 10px solid">
            <%
            } else {
            %>
            <img src="image/default_icon.png" class="img-circle" width=170px height=170px
                 style="border: darkorange 10px solid">
            <%
                }
            %>
            <div class="caption">
                <%
                    if (count == 1) {
                %>
                <p class="first text-center">1</p>
                <%
                } else if (count == 2) {
                %>
                <p class="second text-center">2</p>
                <%
                } else {
                %>
                <p class="third text-center">3</p>
                <%
                    }
                %>
                <br>
                <table class="table table-striped information text-center">
                    <tr>
                        <td>队伍名</td>
                        <td><%=item.getUserName()%>
                        </td>
                    </tr>
                    <tr>
                        <td>解题数目</td>
                        <td><%=item.getSolveCount()%>
                        </td>
                    </tr>
                    <tr>
                        <td>有效提交次数</td>
                        <td><%=item.getSolveCount() + item.getNotSolveCount()%>
                        </td>
                    </tr>
                    <tr>
                        <td>总提交次数</td>
                        <td><%=item.getAllCount()%>
                        </td>
                    </tr>
                    <tr>
                        <td>最后一次通过时间</td>
                        <td><%=item.getLastSubmitTime()%>
                        </td>
                    </tr>
                    <tr>
                        <td>总耗时</td>
                        <td><%=item.getAllTime()%>
                        </td>
                    </tr>
                </table>
            </div>
        </a>
    </div>
    <%
            }
        }
        userServer.close();
        fileServer.close();
    %>
</div>
<table class="table table-striped word_break">
    <tr>
        <td class="text-center"><b>排名</b></td>
        <td class="text-center"><b>队伍名</b></td>
        <td class="text-center"><b>解题数目</b></td>
        <td class="text-center"><b>有效提交次数</b></td>
        <td class="text-center"><b>总提交次数</b></td>
        <td class="text-center"><b>最后一次通过时间</b></td>
        <td class="text-center"><b>总耗时</b></td>
        <td class="text-center"><b>查看提交</b></td>
    </tr>
    <%
        for (; i < userRanks.size(); i++) {
            item = userRanks.get(i);
            if (item.getAllCount() != 0) {
    %>
    <tr>
        <td class="text-center">
            <h4>
                <%=(++count)%>
            </h4>
        </td>
        <td class="text-center"><%=item.getUserName()%>
        </td>
        <td class="text-center"><%=item.getSolveCount()%>
        </td>
        <td class="text-center"><%=item.getSolveCount() + item.getNotSolveCount()%>
        </td>
        <td class="text-center"><%=item.getAllCount()%>
        </td>
        <td class="text-center"><%=item.getLastSubmitTime()%>
        </td>
        <td class="text-center"><%=item.getAllTime()%>
        </td>
        <td class="text-center">
            <form method="post" action="details.jsp" target="_blank">
                <input type="text" name="user" value="<%=item.getUserName()%>" hidden="hidden">
                <input type="submit" class="btn btn-info" value="查看">
            </form>
        </td>
    </tr>
    <%
            }
        }
        rankServer.close();
    %>
</table>
<br>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/page.js"></script>
<script src="js/ver_code.js"></script>
</html>
